package com.logic.landseaserver.service.impl;


import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.logic.common.exception.BusinessException;
import com.logic.common.security.SecurityUtils;
import com.logic.common.util.QueryUtil;
import com.logic.landseaserver.common.util.DateUtil;
import com.logic.landseaserver.domain.RemouldProject;
import com.logic.landseaserver.domain.ReportStrip;
import com.logic.landseaserver.persistence.read.RemouldProjectReadMapper;
import com.logic.landseaserver.persistence.read.ReportStripReadMapper;
import com.logic.landseaserver.persistence.write.ReportStripWriteMapper;
import com.logic.landseaserver.service.IReportStripService;
import com.logic.landseaserver.ws.dto.DepartmentDTO;
import com.logic.landseaserver.ws.dto.ReportStripDTO;
import com.logic.landseaserver.ws.dto.WeekReportStripDTO;

@Service
public class ReportStripServiceImpl implements IReportStripService{

	@Autowired
	private ReportStripReadMapper reportStripReadMapper;

	@Autowired
	private ReportStripWriteMapper reportStripWriteMapper;
	
	@Autowired
	private RemouldProjectReadMapper remouldProjectReadMapper;

	public List<ReportStripDTO> queryreportStrip(Integer id,String type){

		List<ReportStripDTO> result = new ArrayList<ReportStripDTO>();

		List<ReportStripDTO> monthReportStrip  = new ArrayList<>();
		HashMap<String, Object> map = new HashMap<>();
		//查出所有月报
		monthReportStrip = reportStripReadMapper.queryreportStrip(id, type);
		
		Set<Integer> set = new HashSet<>();
		for (ReportStripDTO reportStripDTO : monthReportStrip) {
			set.add(reportStripDTO.getMonth());
		}
		for (Integer month : set) {			
			List<DepartmentDTO> departmentDTOs = new ArrayList<>();
			
			List<WeekReportStripDTO> weekReportStrip  = new ArrayList<>();
			List<WeekReportStripDTO> weekReportStrip1  = new ArrayList<>();
			for (ReportStripDTO reportStripDTO : monthReportStrip) {
				DepartmentDTO departmentDTO = new DepartmentDTO();
				if (month.intValue() == reportStripDTO.getMonth().intValue()) {
					departmentDTO.setId(reportStripDTO.getId());
					departmentDTO.setDepartmentName(reportStripDTO.getDepartment());
					departmentDTOs.add(departmentDTO);					
				}				
			}
			map.put("month", month);
			map.put("id", id);
			map.put("type", "周报");
			weekReportStrip = reportStripReadMapper.queryWeekReportStrip(map);
			Set<Integer> set1 = new HashSet<>();
			for (WeekReportStripDTO weekReportStripDTO : weekReportStrip) {
				set1.add(weekReportStripDTO.getWeek());
			}
			for (Integer week : set1) {
				List<DepartmentDTO> departmentDTOs1 = new ArrayList<>();
				for (WeekReportStripDTO weekReportStripDTO : weekReportStrip) {
					if (week.intValue() == weekReportStripDTO.getWeek().intValue()) {
						DepartmentDTO departmentDTO1 = new DepartmentDTO();
						departmentDTO1.setId(weekReportStripDTO.getId());
						departmentDTO1.setDepartmentName(weekReportStripDTO.getDepartment());
						departmentDTOs1.add(departmentDTO1);
					}					
				}				
				WeekReportStripDTO weekReportStripDTO = new WeekReportStripDTO();
				weekReportStripDTO.setWeek(week);
				weekReportStripDTO.setType("周报");
				weekReportStripDTO.setDepartmentList(departmentDTOs1);
				weekReportStrip1.add(weekReportStripDTO);
				
			}
			
			RemouldProject remouldProject = new RemouldProject();
			remouldProject = remouldProjectReadMapper.selectByPrimaryKey(id);			
			
			ReportStripDTO reportStripDTO = new ReportStripDTO();
			reportStripDTO.setMonth(month);
			reportStripDTO.setType("月报");
			reportStripDTO.setSerialNum(remouldProject.getSerialNum());
			reportStripDTO.setName(remouldProject.getName());
			reportStripDTO.setDepartmentList(departmentDTOs);
			reportStripDTO.setWeekReport(weekReportStrip1);
					
			result.add(reportStripDTO);
				
		}
		return result;
	}
	@Override
	public void delete(String arg0) throws BusinessException {
		String[] idArr = arg0.split(",");
		for (String id : idArr) {
			Integer reportStripId = Integer.valueOf(id);
			ReportStrip reportStrip = (ReportStrip) this.getObject(reportStripId);
			Boolean pass = this.validateForDelete(reportStrip);
			if (pass) {
				reportStrip.updateCommonInfo(SecurityUtils.getCurrentLogin().getUserId());
				reportStripWriteMapper.deleteByPrimaryKey(reportStrip);
			}
		}
	}

	@Override
	public Object get(Integer arg0) throws BusinessException {
		return reportStripReadMapper.selectByPrimaryKey(arg0);
	}
	
	public Object getObject(Integer arg0) throws BusinessException {
		return reportStripReadMapper.selectByPrimaryKeyR(arg0);
	}

	@Override
	public int insert(Object arg0) throws BusinessException {
		Integer id = -1;
		Boolean pass = this.validateForInsert(arg0);
		if (pass) {
			ReportStrip reportStrip = (ReportStrip) arg0;
			reportStrip.updateCommonInfo(SecurityUtils.getCurrentLogin().getUserId());
			Date date  = new Date();
			DateUtil dateUtil = new DateUtil();
			reportStrip.setTotalWeekOfYear(dateUtil.getWeekOfYear(date));
			id = reportStripWriteMapper.insertSelective(reportStrip);
			
		}
		return id;
	}

	@Override
	public List<?> query(QueryUtil arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void update(Object arg0) throws BusinessException {
		

	}

	@Override
	public Boolean validate(Object arg0) throws BusinessException {
		Boolean pass = true;
		return pass;
	}

	@Override
	public Boolean validateForDelete(Object arg0) throws BusinessException {
		return true;
	}

	@Override
	public Boolean validateForInsert(Object arg0) throws BusinessException {
		Boolean pass = false;
		pass = this.validate(arg0);
		return pass;
	}

	@Override
	public Boolean validateForUpdate(Object arg0) throws BusinessException {
		Boolean pass = false;
		pass = this.validate(arg0);
		return pass;	}

}
